package leader

import (
	"fmt"
	"strconv"
	"time"

	"github.com/xuri/excelize"
)

// Update 更新所内领导页
func Update(xlsx *excelize.File, totalIndex string, leaderIndex string, tTradeDate time.Time, workLine int, lastLine int, totalLine string) {
	// 临时变量
	var col string
	var style int
	var value string
	var iValue int
	var fValue float64
	var errstyle error
	var errv error

	fmt.Println("更新表格 ...")

	// 更新 大连累计
	col = "E" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "F" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(leaderIndex, col, col, style)
	}

	// 更新 郑州累计
	col = "F" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "G" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 上海累计
	col = "G" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "H" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 三市合计
	col = "H" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "I" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 交易日
	col = "I" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "J" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 大商所累计成交量占比
	col = "J" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "K" + totalLine
	fValue, errv = strconv.ParseFloat(value, 64)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, fValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 大商所日均成交量
	col = "K" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "L" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 当日合计
	col = "L" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "M" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 上期所累计成交量占比
	col = "M" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "N" + totalLine
	fValue, errv = strconv.ParseFloat(value, 64)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, fValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新  郑商所累计成交量占比
	col = "N" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "O" + totalLine
	fValue, errv = strconv.ParseFloat(value, 64)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, fValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 上期所日均成交量
	col = "O" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "P" + totalLine
	fValue, errv = strconv.ParseFloat(value, 64)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, fValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}

	// 更新 郑商所日均成交量
	col = "P" + strconv.Itoa(workLine)
	value = xlsx.GetCellValue(totalIndex, col)
	col = "Q" + totalLine
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(leaderIndex, col, iValue)
	} else {
		xlsx.SetCellValue(leaderIndex, col, value)
	}
}
